//
//  OwlSupport.m
//  Owl
//
//  Created by Tianzhou on 09-2-20.
//  Copyright 2009 Tianzhou Software Inc.. All rights reserved.
//

#import "OwlSupport.h"
#import "OwlPreferenceKeys.h"
#import "GTMNSFileManager+Path.h"
#import "GTMNSEnumerator+Filter.h"

NSString *const kFileTypeDelimiter = @";";

@interface NSString(OwlNSString)
- (BOOL)hasSuffixInCandidateSuffixList:(NSString *)suffixList;
@end

@implementation NSString(OwlNSString)
- (BOOL)hasSuffixInCandidateSuffixList:(NSString *)suffixList
{
	int i;
	NSArray *suffixArray = [suffixList componentsSeparatedByString:kFileTypeDelimiter];
	for (i = 0;i < [suffixArray count]; ++i)
	{
		if ([[suffixArray objectAtIndex:i] length] > 0
			&& [self hasSuffix:[suffixArray objectAtIndex:i]])
		{
			return YES;
		}
	}
	return NO;
}
@end

@implementation OwlSupport

+ (NSArray *)interestedFilesUnderPath:(NSString *)thePath
{
	NSFileManager *fileManager = [NSFileManager defaultManager];
	NSDirectoryEnumerator *directoryEnum = [fileManager enumeratorAtPath:thePath];
	
	NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
	NSString *suffixList = [userDefaults objectForKey:kOwlSuffixListKey];
	
	NSEnumerator *enumerator2 =
	[directoryEnum gtm_filteredEnumeratorByMakingEachObjectPerformSelector:@selector(hasSuffixInCandidateSuffixList:)
																withObject:suffixList];
	return [enumerator2 allObjects];
}

@end
